package iris.flex.logic
{
	import flash.display.DisplayObject;
	
	import mx.core.Application;
	import mx.core.IFlexDisplayObject;
	import mx.effects.Blur;
	import mx.effects.Effect;
	import mx.events.EffectEvent;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;
	
	public class AdvancedPopUpManager
	{
		public static const EFFECT_BLUR:String = "effectBlur";
		
		public static const LOCATE_MOUSE:String = "locateMouse";
		public static const LOCATE_PARENT_CENTER:String = "locateParentCenter";
		
		private var _popUp:IFlexDisplayObject;
		
		private var _className:Class;
		private var _modal:Boolean;
		private var _parent:DisplayObject;
		
		private var _locate:String;
		
		private var _createEffect:Effect;
		private var _removeEffect:Effect;
		
		public function AdvancedPopUpManager(className:Class, modal:Boolean, parent:DisplayObject, locate:String, effect:String)
		{
			this._className = className;
			this._modal = modal;
			this._parent = parent;
			
			this._locate = locate;
			
			if(effect == EFFECT_BLUR)
			{
				var eC:Blur = new Blur();
				eC.blurYTo = 0;
				eC.blurXTo = 0;
				eC.blurXFrom = 18;
				eC.blurYFrom = 18;
				eC.duration = 500;
				
				_createEffect = eC;
				
				var eR:Blur = new Blur();
				eR.blurYTo = 12;
				eR.blurXTo = 12;
				eR.duration = 500;
				
				_removeEffect = eR;
			}
		}
		public function createPopUp():IFlexDisplayObject
		{
			_popUp = PopUpManager.createPopUp(_parent, _className, _modal);
			
			_popUp.addEventListener(FlexEvent.CREATION_COMPLETE, popUpCreationComplete);
			
			return _popUp;
		}
		private function popUpCreationComplete(event:FlexEvent):void
		{
			if(_locate == LOCATE_MOUSE)
			{
				_popUp.x = Application.application.mouseX;
				_popUp.y = Application.application.mouseY;
			}
			else if(_locate == LOCATE_PARENT_CENTER)
			{
				PopUpManager.centerPopUp(_popUp);
			}
			
			_createEffect.play([_popUp]);
		}
		public function removePopUp():void
		{
			_removeEffect.play([_popUp]);
			
			_removeEffect.addEventListener(EffectEvent.EFFECT_END, removeEffectEnd);
		}
		private function removeEffectEnd(event:EffectEvent):void
		{
			PopUpManager.removePopUp(_popUp);
			//_createEffect.play([_popUp]);
		}
	}
}